(function() {
    base2Cells.util.namespace("base2Cells.ytPlaylist");

    // ---- Imports ----
    var Item;
    var Global;

    try {
        Item = base2Cells.ytPlaylist.Item;
        Global = base2Cells.ytPlaylist.Global;
    } catch(e) {
        alert("Import Error: Could not find necessary libraries within ItemContainer.");
    }

    /**
     * @param cfg The configuration object.
     * <pre>
     *  {
     *      itemList: [{    A list of items
     *          artist: The artist name.
     *          title: The title of the video.
     *          ytUrl: The youtube url.
     *      }, ...]
     *      ownerCt: The owner container.
     *  }
     * </pre>
     */
    base2Cells.ytPlaylist.ItemContainer = function(cfg) {
        this.itemList = [];
        this.el = null;

        var idGenerator = 0;

        // Constructor
        (function(cfg) {
            this.el = document.createElement('div');
            this.el.setAttribute('class', 'itemContainer');
            this.el.setAttribute('id', 'itemContainer-id');

            // Generate the title for this application.
            var titleEl = document.createElement('div');
            titleEl.setAttribute('class', 'itemContainerTitle');
            titleEl.setAttribute('id', 'itemContainerTitle-id');
            titleEl.innerHTML = "YouTube Playlist Gadget";

            this.addSongEl = document.createElement('div');
            this.addSongEl.setAttribute('class', 'addSong');
            this.addSongEl.setAttribute('id', 'itemContainerAddSong-id');

            var addSongTextEl = document.createElement('span');
            addSongTextEl.setAttribute('class', 'addSongText');
            addSongTextEl.innerHTML = "Add";
            addSongTextEl.onclick = function() {
                base2Cells.ytPlaylist.AddWindow.show();
            }

            this.addSongEl.appendChild(addSongTextEl);

            this.el.appendChild(titleEl);
            this.el.appendChild(this.addSongEl);

            this.list = cfg.itemList;
            var thisItemList = this.itemList;

            var scope = this;
            this.list.forEach(function(item) {
                thisItemList.push(new Item({
                    id: idGenerator++,
                    artist: item.artist,
                    title: item.title,
                    ytUrl: item.ytUrl,
                    ownerCt: scope.el
                }));
            });

            renderItems.apply(this);
            Global.itemContainer = this;
        }).apply(this, [cfg]);

        /**
         * Apply this element to the specified element identifier.
         */
        this.applyTo = function(elId) {
            var element = document.getElementById(elId);
            element.appendChild(this.el);
        }

        /**
         * Render the items to this element.
         */
        function renderItems() {
            // Clear out old list of children if there are any.
            var thisEl = this.el;
            for (var i = 1; i < this.el.children.length - 1; i++) {
                this.el.removeChild(this.el.children[1]);
            }

            // Insert the new nodes into the list.
            this.itemList.forEach(function(item) {
                thisEl.insertBefore(item.getEl(), thisEl.children[thisEl.children.length - 1]);
            });
        }

        /**
         * Add a song to the list.
         *
         * Called by add window
         */
        this.addSong = function(song) {
            var item = new Item({
                id: idGenerator++,
                artist: song.artist,
                title: song.title,
                ytUrl: song.ytUrl,
                ownerCt: this.el
            });

            for (var i = 0; i < this.itemList.length; i++) {
                if (this.itemList[i].artist == item.artist && this.itemList[i].title == item.title)
                    return false;
            }

            this.itemList.push(item);
            this.el.insertBefore(item.el, this.el.children[this.el.children.length - 1]);

            base2Cells.ytPlaylist.addItem({
                artist: song.artist,
                title: song.title,
                ytUrl: item.ytId
            });

            return true;
        }
    }
})();
